import Vue from "vue";
import VueRouter from "vue-router";
import store from '../vuex/store'
import Axios from '../utils/axios'
Vue.use(VueRouter);
const routes = [
  /* 404 */
  {
    path: '*',
    component: () => import('@/components/404/404'),
    name: 'notfound',
    meta: {
      title: '404',
      icon: 'el-icon-s-home'
    }
  },
  /* 注册 */
  {
    path: '/register',
    component: () => import('@/components/login/registerPage.vue'),
    name: 'register',
    meta: {
      title: '注册',
      icon: 'el-icon-s-home'
    }
  },
  /* 登录 */
  {
    path: '/login',
    component: () => import('@/components/login/loginPage.vue'),
    name: 'login',
    meta: {
      title: '登录',
      icon: 'el-icon-s-home'
    }
  },
  /* 首页 */
  {
    path: '/home',
    component: () => import('@/components/home/home'),
    name: 'home',
    meta: {
      title: '首页',
      icon: 'el-icon-s-home'
    }
  },
  {
    name: 'default',
    path: '/',
    redirect: {
      name: 'home'
    }
  }
]
const marketingRoutes=[/* 营销 */
  {
    path: '/marketing',
    component: () => import('@/components/content'),
    name: 'marketing',
    meta: {
      title: '营销',
      icon: 'el-icon-s-shop'
    },
    children: [
      {
        path: '/marketing/flash',
        component: () => import('@/components/404/404'),
        name: 'flash',
        meta: {
          title: '秒杀活动列表',
          parent: '营销',
          icon: 'el-icon-timer'
        }
      },
      {
        path: '/marketing/coupon',
        component: () => import('@/components/404/404'),
        name: 'coupon',
        meta: {
          title: '优惠券列表',
          parent: '营销',
          icon: 'el-icon-collection-tag'
        }
      },
      {
        path: '/marketing/brand',
        component: () => import('@/components/404/404'),
        name: 'brand',
        meta: {
          title: '品牌推荐',
          parent: '营销',
          icon: 'el-icon-reading'
        }
      },
      {
        path: '/marketing/new',
        component: () => import('@/components/404/404'),
        name: 'new',
        meta: {
          title: '新品推荐',
          parent: '营销',
          icon: 'el-icon-search'
        }
      },
      {
        path: '/marketing/hot',
        component: () => import('@/components/404/404'),
        name: 'hot',
        meta: {
          title: '人气推荐',
          parent: '营销',
          icon: 'el-icon-refresh'
        }
      },
      {
        path: '/marketing/subject',
        component: () => import('@/components/404/404'),
        name: 'subject',
        meta: {
          title: '专题推荐',
          parent: '营销',
          icon: 'el-icon-s-check'
        }
      },
      {
        path: '/marketing/advertise',
        component: () => import('@/components/404/404'),
        name: 'advertise',
        meta: {
          title: '广告列表',
          parent: '营销',
          icon: 'el-icon-s-promotion'
        }
      }
    ],
  }]
const rightsRoutes=[/* 权限 */
  {
    path: '/rightControl',
    component: () => import('@/components/content'),
    name: 'rightControl',
    meta: {
      title: '权限',
      icon: 'el-icon-s-custom'
    },
    children: [
      {
        path: '/rightControl/admin',
        component: () => import('@/components/jurisdiction/Userlist.vue'),
        name: 'admin',
        meta: {
          title: '用户列表',
          parent: '权限',
          icon: 'el-icon-user-solid'
        }
      },
      {
        path: '/rightControl/role',
        component: () => import('@/components/jurisdiction/Rolelist.vue'),
        name: 'role',
        meta: {
          title: '角色列表',
          parent: '权限',
          icon: 'el-icon-user'
        }
      },
      {
        path: '/rightControl/menu',
        component: () => import('@/components/jurisdiction/Menulist.vue'),
        name: 'menu',
        meta: {
          title: '菜单列表',
          parent: '权限',
          icon: 'el-icon-menu'
        }
      },
      {
        path: '/rightControl/resource',
        component: () => import('@/components/jurisdiction/Resourcelist.vue'),
        name: 'resource',
        meta: {
          title: '资源列表',
          parent: '权限',
          icon: 'el-icon-s-data'
        }
      },

    ],
  }];
const orderRoutes=[/* 订单 */
  {
    path: '/order',
    component: () => import('@/components/content'),
    name: 'order',
    meta: {
      title: '订单',
      icon: 'el-icon-s-order'
    },
    children: [
      {
        path: '/order/list',
        component: () => import('@/components/order/orderlist'),
        name: 'orderList',
        meta: {
          title: '订单列表',
          parent: '订单',
          icon: 'el-icon-s-grid'
        }
      },
      {
        path: '/order/setting',
        component: () => import('@/components/order/orderSetting.vue'),
        name: 'orderSetting',
        meta: {
          title: '订单设置',
          parent: '订单',
          icon: 'el-icon-setting'
        }
      },
      {
        path: '/product/returnApply',
        component: () => import('@/components/order/returnApply.vue'),
        name: 'returnApply',
        meta: {
          title: '退货申请处理',
          parent: '订单',
          icon: 'el-icon-position'
        }
      },
      {
        path: '/order/returnReason',
        component: () => import('@/components/order/returnReason.vue'),
        name: 'returnReason',
        meta: {
          title: '退货原因设置',
          parent: '订单',
          icon: 'el-icon-chat-line-round'
        }
      }
    ],
  },] ;
const productRoutes = [/* 商品 */
  {
    path: '/product',
    component: () => import('@/components/content'),
    name: 'product',
    meta: {
      title: '商品',
      icon: 'el-icon-s-goods'
    },
    children: [
      {
        path: '/product/list',
        component: () => import('@/components/product/product.vue'),
        name: 'productList',
        meta: {
          title: '商品列表',
          parent: '商品',
          icon: 'el-icon-menu'
        }
      },
      {
        path: '/product/add',
        component: () => import('@/components/product/addProduct/add'),
        name: 'productAdd',
        meta: {
          title: '添加商品',
          parent: '商品',
          icon: 'el-icon-document-add'
        }
      },
      {
        path: '/product/classify',
        component: () => import('@/components/product/ProductCatalogue.vue'),
        name: 'productClassify',
        meta: {
          title: '商品分类',
          parent: '商品',
          icon: 'el-icon-folder'
        }
      },
      {
        path: '/product/type',
        component: () => import('@/components/product/ProductType.vue'),
        name: 'productType',
        meta: {
          title: '商品类型',
          parent: '商品',
          icon: 'el-icon-edit-outline'
        }
      },
      {
        path: '/product/brand',
        component: () => import('@/components/product/ProductBrand.vue'),
        name: 'productBrand',
        meta: {
          title: '品牌管理',
          parent: '商品',
          icon: 'el-icon-trophy'
        }
      }
    ],
  },];
var router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (store.state.isLogin) {
    next()
  } else {
    var userInfo = JSON.parse(window.localStorage.getItem('userInfo')) || {}
    Axios
      .post("http://49.233.65.169:3000/api/userlog", {
        username: userInfo.username,
        id: userInfo.id
      })
      .then(res => {
        if (res.code === 0) {
          store.commit('loginIn');
          // res.data.authority="订单管理员"
          if(res.data.authority==="超级管理员"){
            router.addRoutes([...orderRoutes,...productRoutes,...rightsRoutes,...marketingRoutes]);
            router.options.routes=[...routes,...orderRoutes,...productRoutes,...rightsRoutes,...marketingRoutes]
          }else if(res.data.authority==="商品管理员"){
            router.addRoutes([...productRoutes,...marketingRoutes]);
            router.options.routes=[...routes,...productRoutes,...marketingRoutes]
          }else if(res.data.authority==="订单管理员"){
            router.addRoutes([...orderRoutes,...marketingRoutes]);
            router.options.routes=[...routes,...orderRoutes,...marketingRoutes]
          }else{
            router.options.routes=routes
          }
          next(to.path)
        } else if (to.path != '/login' && to.path != '/register') {
          next('/login')
        } else {
          next()
        }   
      })
      .catch(err => err);
  }
})
export default router